﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace MainJumpFleet.TempObjects
{
    public class ship
    {


        Vector2 position;
        Vector2 origin;
        public Vector2 velocity;
        public Vector2 acceleration;
        public Vector2 dir;
        float rotation;
        float thrustRate;
        public float rotationRate;
        float rotationOffset;
        public float speed;
        float turnSpeed;
        float smoothStop;
        public Vector2 forwardVec;
        public Rectangle Bounds;
        SpriteFont font;
        List<Bullet> b = new List<Bullet>();
        // holds the camera object
        OakEngine.Camera.Oak_Camera camera;

        JumpFleetGameLib.xSprite.StaticSprite sprite;
        bool visible;

        #region PROPERTIES

        public bool Visible
        {
            get { return visible; }

            set
            {
                visible = value;

                sprite.Visible = value;
            }
        }

        public Vector2 Position
        {
            get { return position; }

            set
            {
                position = value;
                sprite.DrawPosition = value;
            }
        }

        public float Rotation
        {
            get { return rotation; }

            set
            {
                rotation = value;

                sprite.RotationAmount = value;
            }
        }


        #endregion

        /*
       public ship(Texture2D tex,SpriteFont font,ref OakEngine.Camera.Oak_Camera cam)
       {
           texture = tex;
           origin = new Vector2(texture.Width * 0.5f, texture.Height * 0.5f);
           rotationOffset = 1.6f;
           speed = 0;
           smoothStop = 1;
           thrustRate = 100.0f;
           acceleration = new Vector2();
           rotationRate = (float) 3.0f;
           this.font = font;
           forwardVec = new Vector2(0, -1);
           camera = cam;
       }
         */

        public ship(JumpFleetGameLib.xSprite.StaticSprite sprite, SpriteFont font, ref OakEngine.Camera.Oak_Camera cam)
        {
            this.sprite = sprite;
            origin = sprite.RotationOriginPoint;
            rotationOffset = 1.6f;
            speed = 0;
            smoothStop = 1;
            thrustRate = 100.0f;
            acceleration = new Vector2();
            rotationRate = (float)3.0f;
            this.font = font;
            forwardVec = new Vector2(0, -1);
            camera = cam;
            Visible = false;
        }

        public void SetLeftRotation(float dt)
        {
            Rotation -= rotationRate * dt;
            Rotation = OakEngine.AI.Tools.OAK_AITools.WrapAngle(Rotation);
        }
        public void SetRightRotation(float dt)
        {
            Rotation += rotationRate * dt;
            Rotation = OakEngine.AI.Tools.OAK_AITools.WrapAngle(Rotation);
        }


        public bool Intersect(Rectangle rect)
        {
            foreach (var bull in b)
            {
                if (bull.Bounds.Intersects(rect))
                    return true;
            }
            return false;
        }

        public void Fire(JumpFleetGameLib.xSprite.StaticSprite bulletSprite, Vector2 direction)
        {
            // first check if we have one that isnt alive and if we do have one then lets use it
            foreach (var bul in b)
            {
                if (!bul.IsAlive)
                {
                    bul.Position = Position;
                    bul.velocity = direction;
                    bul.IsAlive = true;
                    bul.Rotation = Rotation;
                    return;
                }
            }

            // if we dont have any to reuse then we should make a new one
            Bullet bull = new Bullet(bulletSprite, Position, direction, Rotation, ref camera);
            b.Add(bull);
        }

        public void Update(float dt)
        {

            foreach (var bull in b)
            {
                // track the bullet if we are far then reset position and disappear
                if (Vector2.Distance(bull.Position, Position) > 500 && bull.IsAlive)
                {
                    bull.IsAlive = false;
                    bull.Position = Position;
                    bull.velocity = Vector2.Zero;
                }

                if (bull.IsAlive)
                    bull.Update(dt);
            }


            this.Bounds = new Rectangle((int)Position.X, (int)Position.Y, 200, 200);

            Position += velocity * dt;
            // sends the movement distance to the camera
            Vector2 temp = new Vector2(speed, speed);
        }

        public void Draw(SpriteBatch batch)
        {
            /*
            foreach (var bull in b)
            {
                if(bull.m_bisAlive)
                bull.Render(batch);
            }
            */
            // batch.Draw(texture, position - camera.CameraPosition, null, Color.White, roation, origin, 1, SpriteEffects.None, 0.3f);

        }


    }
}
